<template>
  <div class="container">
    <div class="app-container" style="padding: 25px;">
      <el-button type="primary" @click="elDialog">添加角色</el-button>
      <el-table :data="tableData" stripe size="medium" fit>
        <el-table-column prop="name" label="角色" width="100">
          <template #default="{row}">
            <span v-if="!row.isEdit">{{ row.name }}</span>
            <el-input v-else v-model="row.temp.name" />
          </template>
        </el-table-column>

        <!-- state是1 显示 启用 否则未启用 -->
        <!-- <el-table-column prop="state" label="是否启用" width="100" /> -->
        <el-table-column label="是否启用" width="100">
          <template #default="{row}">
            <span v-if="!row.isEdit">{{ row.state === 1 ? '启用' : '未启用' }}</span>
            <el-switch v-else v-model="row.temp.state" active-value="1" inactive-value="0" /></template>
        </el-table-column>

        <el-table-column prop="description" label="描述">
          <template #default="{row}">
            <span v-if="!row.isEdit">{{ row.description }}</span>
            <el-input v-else v-model="row.temp.description" />
          </template>
        </el-table-column>

        <el-table-column label="操作">
          <!-- 通过默认插槽指定结构 -->
          <!--  -->
          <!-- 文字按钮 -->
          <template #default="{row}">
            <!-- {{ row }} -->
            <!-- obj是这个组件的作者在封装el-table-column组件时，传递给默认插槽的参数 -->
            <div v-if="!row.isEdit">
              <el-button type="text">分配权限</el-button>
              <el-button type="text" @click="btnOkoK(row)">编辑</el-button>
              <el-popconfirm
                title="这是一段内容确定删除吗？"
                @onConfirm="del(row.id)"
              >
                <template #reference>
                  <el-button type="text">删除</el-button>
                </template>

              </el-popconfirm>
            </div>

            <div v-else>
              <el-button type="primary" @click="add(row)">确定</el-button>
              <el-button @click="Cancel(row)">取消</el-button>
            </div>

          </template>

        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <template>
        <el-pagination
          style="width: 100%; margin-top: 20px; height: 60px; align-items: center; justify-content: end; display: flex"
          :current-page="page"
          :page-sizes="[10, 20, 30, 40]"
          :page-size="pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </template>
      <!-- 新增角色对话框 -->
      <el-dialog title="新增角色" :visible.sync="showDialog" width="500px" @close="close">
        <template #title>新增</template>

        <el-form :ref="'Form'" label-width="100px" :model="loginForm" :rules="rulesForm">
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="loginForm.name" />
          </el-form-item>
          <el-form-item label="启用" prop="state">
            <!-- <el-input></el-input> -->
            <el-switch
              v-model="loginForm.state"
              active-color="#13ce66"
              inactive-color="#ff4949"
              active-icon-class="el-icon-check"
              inactive-icon-class="el-icon-close"
              :active-value="1"
              :inactive-value="0"
            />

          </el-form-item>
          <el-form-item label="角色描述" prop="description">
            <el-input v-model="loginForm.description" type="textarea" />
          </el-form-item>
        </el-form>

        <template #footer>
          <el-button size="mini" type="primary" @click="addForm">确定</el-button>
          <el-button size="mini" @click="close">取消</el-button>
        </template>
      </el-dialog>
    </div>
  </div>
</template>
<script>
import { getRoleList, addRole, updateRole, deleteRole } from '@/api/role'
export default {
  name: 'Role',
  data() {
    return {
      isEdit: false,
      showDialog: false,
      total: 0,
      tableData: [],
      page: 1,
      pagesize: 5,
      loginForm: {
        name: '',
        description: '',
        state: '1'
      },
      rulesForm: {
        name: [
          { required: true, message: '请输入角色名', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '请输入角色描述', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    // 获取列表
    async getList() {
      const result = await getRoleList(this.page, this.pagesize)
      this.tableData = result.rows
      this.total = result.total
      // 响应式数据
      this.tableData.forEach((item) => {
        this.$set(item, 'isEdit', false)
        // 响应式数据 备份数据绑定输入框
        this.$set(item, 'temp', {
          name: item.name,
          description: item.description,
          state: item.state
        })
      })
    },
    // 确定  修改
    async btnOkoK(row) {
      row.isEdit = true
      await updateRole({ ...row.temp, id: row.id })
      this.$message.success('修改成功')
      this.getList()
    },
    // 取消 回显
    Cancel(row) {
      row.temp.name = row.name
      row.temp.description = row.description
      row.temp.state = row.state
      row.isEdit = false
    },
    // 删除
    async del(id) {
      await deleteRole(id)
      this.$message.success('删除成功')
      if (this.page > 1 && this.tableData.length === 1) this.page--
      this.getList()
    },
    // 页码变化
    handleCurrentChange(value) {
      this.page = value
      this.getList()
    },
    // 条数
    handleSizeChange(value) {
      this.pagesize = value
      this.getList()
      // important
      this.page = 1
    },
    elDialog() {
      this.showDialog = true
    },
    // 确认
    addForm() {
      this.$refs.Form.validate(async isOk => {
        if (isOk) {
          await addRole(this.loginForm)
          this.$message.success('添加成功')
          this.close()
          this.getList()
        }
      })
    },
    close() {
      this.showDialog = false
      this.$refs.Form.resetFields()
      this.addForm = {
        name: '',
        description: '',
        state: '0'
      }
    }
  }
}
</script>

